package jblip.gui.components.view;

import java.awt.Desktop;
import java.io.IOException;
import java.net.URISyntaxException;

import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.event.HyperlinkEvent.EventType;

import jblip.gui.JBlip;
import jblip.gui.data.channels.TagDataChannel;
import jblip.gui.data.channels.UserDataChannel;
import jblip.resources.Update;


public class TextViewPaneHyperlinkListener implements HyperlinkListener{
	private final TextViewPane owner;
	public TextViewPaneHyperlinkListener(TextViewPane owner) {
		this.owner = owner;
	}
	@Override
	public void hyperlinkUpdate(HyperlinkEvent e) {
		String description = e.getDescription();
		if (description.length() < 1) {
			System.err.println("(!!) Bad link description - shorter than 1 char");
			return;
		}
		if (e.getEventType() == EventType.ACTIVATED) {
//			link activated
			if (description.charAt(0) == '^' ) {
				JBlip.instance().getChannelFrame().viewChannel(UserDataChannel.getUserChannel(description.substring(1)));
			}
			else if (description.charAt(0) == '#') {
				JBlip.instance().getChannelFrame().viewChannel(TagDataChannel.getTagChannel(description.substring(1)));
			}
			else if (description.charAt(0) == '@') {
				System.err.println("STUB(update reference action)");
//				TODO(lamvak): update reference action
//				JBlip.instance().getChannelFrame().viewChannel(UpdateDataChannel.getUpdateChannel(description.substring(1)));
			}
			else {
				try {
					System.err.print("Trying to fire default browser to open activated link: ");
					Desktop.getDesktop().browse(e.getURL().toURI());
				} catch (IOException e1) {
					System.err.println("IOException");
					return;
				} catch (URISyntaxException e1) {
					System.err.println("URISyntaxException");
					return;
				}
				System.err.println("done");
			}
		}
		else if(e.getEventType() == EventType.ENTERED) {
//			link entered
			if (description.charAt(0) == '^' ) {
//				TODO(lamvak): user reference action; would be nice to add more data, but the API sux
				owner.setToolTipText(description);
			}
			else if (description.charAt(0) == '#') {
//				TODO(lamvak): tag reference action
//				tag tooltip (maybe last update for the tag, if any? API sux, again)
				owner.setToolTipText(description);
			}
			else if (description.charAt(0) == '@') {
//				TODO(lamvak): update reference action - should get eager, now is lazy
				Update msg = JBlip.instance().getBlipClient().getMessage(new Integer(description.substring(1)));
				String tipText = msg.getSrcUserLogin();
				String dstUsr;
				if ((dstUsr = msg.getDstUserLogin()) != null) {
					tipText += " > " + dstUsr;
				}
				tipText +=  ": " + msg.getBody();
				owner.setToolTipText(tipText);
			}
			else if (e.getURL() != null) {
				owner.setToolTipText(e.getURL().toString());
				System.err.println("<link><URL>" + e.getURL() + "</url>");
				System.err.println("<description>" + e.getDescription() + "</description></link>");
			}
			else {
				System.err.println("(!!) Bad link description; URL=NULL, and yet description starts with '" + description.charAt(0) + "' char");
				return;
			}
		}
		else if(e.getEventType() == EventType.EXITED) {
			owner.setToolTipText(null);
		}
	}
}
